package com.cn.designpatterns.ex.concrete.proxy.cglib;

import net.sf.cglib.proxy.Enhancer;
import net.sf.cglib.proxy.InvocationHandler;

import java.lang.reflect.Method;

/**
 * @author zhangyi
 * @date 2018/12/25 16:01
 */
public class CglibProxy implements InvocationHandler{
    private Object object;

    public CglibProxy(Object object) {
        this.object = object;
    }

    public Object bindObject(){
        Enhancer enhancer = new Enhancer();
        enhancer.setSuperclass(object.getClass());
        enhancer.setInterfaces(object.getClass().getInterfaces());
        enhancer.setCallback(this);
        return enhancer.create();
    }

    @Override
    public Object invoke(Object o, Method method, Object[] args) throws Throwable {
        System.out.println("Cglib 处理前...");
        Object invoke = method.invoke(object, args);
        System.out.println("Cglib 处理后...");
        return invoke;
    }
}
